#include "memorygame.h"
#include "ui_memorygame.h"
#include<qrect.h>
memoryGame::memoryGame(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::memoryGame),fsize(30)//碎片大小
{
    ui->setupUi(this);
    setWindowTitle("记忆游戏");
    inPerson();
    ui->backToHome->hide();
    // 获取“再玩一次”按钮，并连接它的clicked信号到槽函数
    ui->startBtn->hide();
    startBtn = ui->startBtn;
    connect(startBtn, SIGNAL(clicked()), this, SLOT(restartGame()));

}
void memoryGame::acceptSystem(){
    this->show();
};
memoryGame::~memoryGame()
{
    delete ui;
}
void memoryGame::paintEvent(QPaintEvent*event){
    //update()或程序第一次启动时自动执行，用于画图
    QPainter painter(this);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(13,13,400,500);
    //画人
    painter.setPen(Qt::transparent);//透明边框
    painter.setBrush(Qt::transparent);
    painter.drawRect(Person);
    painter.drawPixmap(Person,QPixmap(":/myImage/p1.png"));//设置图片
    //画碎片
   painter.setPen(Qt::transparent);
//    painter.setBrush(Qt::green);
    for(int i=0;i<num;i++){
    painter.drawRect(fragment[i]);
    painter.drawPixmap(fragment[i],QPixmap(":/myImage/fragment1.png"));}//设置图片
    if(blsRun&&bombfall==1){
        //绘制碎片上的文字
        for(int i=0;i<num;i++){
            fra_font[i].setStyleName("Courier");
            fra_font[i].setBold(1);
            fra_font[i].setPointSize(10);
            painter.setFont(fra_font[i]);
            painter.setPen(Qt::black);
            if(text[i].size()>7)
                painter.drawText(fragment[i].x()-3,fragment[i].y()+20, text[i]); // 在碎片上方绘制文字
            else
                painter.drawText(fragment[i].x()+2,fragment[i].y()+20, text[i]);
        }
       }
    //画炸弹
    painter.setPen(Qt::transparent);
    painter.setBrush(Qt::red);
    painter.drawRect(bomb);
    painter.drawPixmap(bomb,QPixmap(":/myImage/bomb.png"));//设置图片
    //显示游戏开始/结束
    QFont font1("Courier",20);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::green);
    if(blsOver){
        pDisplay="游戏结束";

    }
    painter.drawText(160,220,pDisplay);
    if(blsOver)//游戏结束，停止计时器
    {
        timer->stop();
        gameOver();
    }
    //得分显示
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(140,275,scoreLabel);
    painter.drawText(235,275,QString::number(score));
};
//初始化人
void memoryGame::inPerson(){
    score=0;
    blsRun=true;
    blsOver=false;
    pDisplay="游戏开始！";
    score=0;
    scoreLabel="得分";
    fragment=createFragment();
    bomb=createSingleFragment();//创建炸弹
    psize=35;
    QRect rect(200,490,psize,psize);//人的初始位置
    Person=rect;//将矩形赋值给人
    for(int i=0;i<num;i++){
     speed[i]=qrand()%5+2;//随机掉落速度

    }
    timer=new QTimer(this);//设定定时器
    timer->start(30);//计时时间是s
    connect(timer,SIGNAL(timeout()),SLOT(personUpdate()));
}
void memoryGame::personUpdate(){
    pDisplay="";

    fraMove();
    isWall_hit();
    isBombHit();
    update();//painterEvent更新
};
//初始创建碎片群
QRect*memoryGame::createFragment(){
    int x,y;
    QRect*a=new QRect[num];
    for(int i=0;i<num+1;i++){
        x=qrand()%350;
        y=13;
        a->setRect(40+x,y,fsize+10,fsize);
    }
    return a;
};
//创建单个碎片
QRect memoryGame::createSingleFragment(){
    int x,y;
    x=qrand()%360;
    y=13;

    QRect rect(20+x,y,fsize+10,fsize);
    return rect;
}
//碎片和炸弹掉落
void memoryGame::fraMove(){
    for(int i=0;i<num;i++){
    fragment[i].setBottom(fragment[i].bottom()+speed[i]);
    fragment[i].setTop(fragment[i].top()+speed[i]);}
    bomb.setBottom(bomb.bottom()+speed[4]);
    bomb.setTop(bomb.top()+speed[4]);
}
void memoryGame::isWall_hit(){
    //如果到底部
    for(int i=0;i<num;i++){
    if(fragment[i].bottom()>520){
        fragment[i]=createSingleFragment();
        text[i]=selectItem();
    }
    //如果碎片底部与人相撞
    else if(fragment[i].bottom()>Person.top()){
        if(fragment[i].left()<Person.left()&&fragment[i].right()>Person.left()){
            speed[i]=qrand()%5+2;
            score+=10;
            fragment[i]=createSingleFragment();//创建新碎片
            text[i]=selectItem();//随机碎片上文字
        }
        else if(fragment[i].right()>Person.right()&&fragment[i].left()<Person.right()){
            speed[i]=qrand()%5+2;
            fragment[i]=createSingleFragment();
            text[i]=selectItem();//随机碎片上文字
            score+=10;
        }
        else if(fragment[i].right()<=Person.right()&&fragment[i].left()>=Person.left()){
            speed[i]=qrand()%5+2;
            fragment[i]=createSingleFragment();
             text[i]=selectItem();//随机碎片上文字
            score+=10;
           }
    }
    }

}
void memoryGame::isBombHit(){

    if( bomb.bottom()>520){
        speed[4]=qrand()%4+3;;
        bomb=createSingleFragment();
        bombfall=1;
    }
    //如果炸弹底部与人相撞
    else if(bomb.bottom()>Person.top()){
        if(bomb.left()<Person.left()&&bomb.right()>Person.left()){
           blsOver=1;
        }
        else if(bomb.right()>Person.right()&&bomb.left()<Person.right()){
            blsOver=1;
        }
        else if(bomb.right()<=Person.right()&&bomb.left()>=Person.left()){
            blsOver=1;
            }
        }

};
void  memoryGame::keyPressEvent(QKeyEvent*event){
    QKeyEvent*key=(QKeyEvent*)event;

    if(blsRun){
    //触墙无移动效果
    switch (key->key()) {
        case Qt::Key_Left:
        //坐标变化
        if(Person.left()>15){
            Person.setLeft(Person.left()-4.5);
            Person.setRight(Person.right()-4.5);}
            break;
        case Qt::Key_Right:
        if(Person.right()<410){
            Person.setLeft(Person.left()+4.5);
            Person.setRight(Person.right()+4.5);
            break;}}

        }
};
void memoryGame::restartGame()
{   ui->startBtn->hide();
    ui->backToHome->hide();
    inPerson();
    // 重新连接键盘事件
    this->setFocus();

}
//游戏结束
void memoryGame::gameOver()
{
    // 显示“再玩一次”按钮
    ui->startBtn->show();
    ui->backToHome->show();
    // 停止计时器
//    timer->stop();
}
//设置物品列表
void memoryGame:: setList(QString*a,int i){
    example=a;
    itemCount = i-1; // 获取example_itemlist中的项数
    for(int i=0;i<num;i++){
     text[i]=selectItem();//随机碎片上文字
    }
}
//随机取物品列表文本
QString memoryGame::selectItem(){
   qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
   int a=0;
   do{
       a=qrand()%itemCount;
   }while(blsSame(example[a]));
   return example[a];
}
//如果画面中碎片有此文本，换一个
bool memoryGame::blsSame(QString w){
    for(int i=0;i<num;i++){
        if(w==text[i]){
            return 1;
        }
    }
    return 0;
}


void memoryGame::on_backToHome_clicked()
{   this->close();
    emit gameToSys();
}
